package com.platform.modules.gen.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.RestResponse;
import com.platform.common.xss.XssHttpServletRequestWrapper;
import com.platform.modules.gen.service.SysGeneratorService;

/**
 * 代码生成器
 */
@Api(tags = "SysGeneratorController|代码生成器管理")
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;

	/**
	 * 分页查询所有表
	 *
	 * @param params
	 *            查询参数    
	 * @return RestResponse
	 */
	@ApiOperation(value = "分页查询代码生成器列表", notes = "分页查询代码生成器列表")
	  @ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
			@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer")
    })
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("sys:generator:list")
	public RestResponse list(Integer currPage,Integer limit) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page",PageUtil.getCurrPage(currPage));
		params.put("limit", PageUtil.getLimit(limit));
		// 查询列表数据
		Page page = sysGeneratorService.queryPage(params);
		return RestResponse.success().put("page", page);
	}

	/**
	 * 生成代码
	 *
	 * @param request
	 *            request
	 * @param response
	 *            response
	 */
	@ApiOperation(value = "生成代码", notes = "生成代码")
	  @ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "tables", value = "数据库表名集合", dataType = "String"),
			@ApiImplicitParam(paramType = "query", name = "projectName", value = "项目名称", dataType = "String"),
			@ApiImplicitParam(paramType = "query", name = "packageName", value = "", dataType = "String"),
			@ApiImplicitParam(paramType = "query", name = "author", value = "", dataType = "String")
    })
	@SysLog("生成代码")
	@PostMapping("/code")
	@RequiresPermissions("sys:generator:code")
	public void code(String tables,String projectName,String packageName,String author,HttpServletRequest request, HttpServletResponse response) throws IOException {

		// 获取表名，不进行xss过滤+
		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
//		String tables = orgRequest.getParameter("tables");
//		String projectName = orgRequest.getParameter("projectName");
//		String packageName = orgRequest.getParameter("packageName");
//		String author = orgRequest.getParameter("author");
		System.err.println("tables===="+tables);
		if(tables ==null){
			tables = "";
		}
		
		String[] tableNames = tables.split("\\,");

		byte[] data = sysGeneratorService.generatorCode(tableNames, projectName, packageName, author);

		response.reset();
		response.setHeader("Content-Disposition",
				"attachment; filename=\"AutoCode" + DateUtils.format(new Date(), "yyyyMMddHHmmss") + ".zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}
}
